import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.ScrollPane;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageWindow extends JFrame {

    private JPanel mImagePanel;
    private BufferedImage mImage;

    public ImageWindow(Point parentLocation, Dimension imageSize, String title) 
    {
        super("Image frame");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(parentLocation.x + 100, parentLocation.y + 100);
        mImagePanel = new JPanel() 
        {

            @Override
            public void paint(Graphics g) 
            {
                super.paint(g);
                if (mImage != null) 
                    g.drawImage(mImage, 0, 0, null);                
            }
        };
        Container con = getContentPane();
        mImagePanel.setPreferredSize(imageSize);
        ScrollPane scroll = new ScrollPane();
        scroll.add(mImagePanel);
        con.add(scroll, BorderLayout.CENTER);
        if (screenSize.height * 0.5 > imageSize.height && screenSize.width * 0.5 > imageSize.width) {
            setSize((int) (imageSize.width + 30), (int) (imageSize.height + 50)); // 30 && 50 because frame has borders and title
                                                                                  // bar (so is bigger than panel) and we want
                                                                                  // panel to be image size and frame a little
                                                                                  // bigger
            setPreferredSize(new Dimension((int) (imageSize.width + 30), (int) (imageSize.height + 50)));
        } else {
            setSize(screenSize.width / 4, screenSize.height / 4);
            setPreferredSize(new Dimension(screenSize.width / 4, screenSize.height / 4));
        }
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setTitle(title);
    }

    public void refreshViewWithImage(BufferedImage im) {
        mImage = im;
        mImagePanel.repaint();
    }
}
